<template>
	<view style="padding-bottom:0;height:calc(100%);background: #fff;">
		<!--part01-->
		<view class="uni-title uni-common-mt">
			<view class="headpic text">
				<view class="uni-flex uni-column" style="justify-content:center;">
					<view class="flex-item flex-item-V ">
						<image :src="src"  @error="imageError" style="width:200upx;height:200upx;" mode="widthFix" lazy-load></image>
					</view>
					<!-- <view class="flex-item flex-item-V"><view style="color:#000;"></view></view> -->
				</view>
				
			</view>

		</view>
		<!--part02-->
		<view class="uni-padding-wrap">
			<button type="primary" plain="true" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">绑定手机号成为品致会员</button>
			<!-- <button type="primary" open-type="getUserInfo" @getuserinfo="loginobj">使用微信授权登录</button> -->
			<fr-loading></fr-loading>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	export default {
		data() {
			return {
				article: null,
				src: "/static/logo.png"
			}
		},
		computed: {
			...mapState(['hasLogin', 'userinfo','inviteCode'])
		},
		onLoad(opt) {
			console.log(this.inviteCode)
			uni.setNavigationBarTitle({ title: '绑定品致会员' });
		},
		methods: {
			imageError: function(e) {
				console.error('image发生error事件，携带值为' + e.detail.errMsg)
			},
			getPhoneNumber(e) {
				console.log(e);
				let that = this
				let url = this.$store.state.base_url
				let usercode = '';
				let userencryptedData = '';
				let useriv = '';
				let usersignature = '';
				let phoneencryptedData = e.detail.encryptedData;
				let phoneiv = e.detail.iv;
				if(e.detail.encryptedData==undefined){
					uni.showModal({ title: '提示', content: '授权失败，可能会影响您对品致小程序的正常使用。', confirmColor: '#000000', showCancel: false,
						success: function(res) {
							if (res.confirm) {
								that.$store.commit("logout");
								setTimeout(() => { uni.navigateBack() }, 1000)
							}
						}
					})
					return;
				}else{
					uni.login({
						provider: 'weixin',
						success: function(loginRes) {
							usercode = loginRes.code;
							//换取用户信息
							
							uni.getUserInfo({
								provider: 'weixin',
								success: function(infoRes) {
									let userinfo =JSON.parse(infoRes.rawData);
									usersignature = infoRes.signature;
									userencryptedData = infoRes.encryptedData;
									useriv = infoRes.iv;
									//console.log(JSON.stringify(infoRes));
									//console.log("usercode@ "+usercode);
									//console.log("usersignature@ "+usersignature);
									//console.log("userencryptedData@ "+userencryptedData);
									//console.log("useriv@ "+useriv);
									//console.log("phoneencryptedData@ "+phoneencryptedData);
									//console.log("phoneiv@ "+phoneiv);
									// let params = {
									// 		"id": 2,
									// 		"method": "register",
									// 		"params": {
									// 			"code": usercode,
									// 			"signature":usersignature,
									// 			"phoneEncryptedData":phoneencryptedData,
									// 			"phoneIv":phoneiv,
									// 			"userEncryptedData":userencryptedData,
									// 			"userIv":useriv,
									// 			"inviteCode": that.inviteCode
									// 		}
									// 	}
									// 	console.log(JSON.stringify(params))
									// 	return;
									
									uni.request({
										url: url,
										method: 'POST',
										data: {
											"id": 2,
											"method": "register",
											"params": {
												"code": usercode,
												"signature":usersignature,
												"phoneEncryptedData":phoneencryptedData,
												"phoneIv":phoneiv,
												"userEncryptedData":userencryptedData,
												"userIv":useriv,
												"inviteCode": that.inviteCode
											}
										},
										success(res) {
											console.log(JSON.stringify(res))
											if (res.data.result.status == 'success') {
												that.$store.commit("login",res.data.result.data);
												that.$store.commit("setMember");
											} else if(res.data.result.status == 'none') {
												that.$store.commit("login",userinfo);						
											}else{
												uni.showToast({ title: res.data.result.data.message, duration: 3000, mask: true, icon: 'none' })
											}
										},
										fail() {
											uni.showToast({ title: "网络不可用，请稍后在试。", duration: 3000, mask: true, icon: 'none' })
										},
										complete() {
											setTimeout(() => {
												//数据
												uni.navigateBack()
											}, 1000)
										}
									})
								},
								fail(e) {
									uni.showModal({ title: '提示', content: '拒绝授权会影响您正常使用。', confirmColor: '#000000', showCancel: false,
										success: function(res) {
											if (res.confirm) {
												setTimeout(() => { uni.navigateBack() }, 1000)
											}
										}
									})

								}
							});
						},
						fail(e) {
							console.log(e)
						},
						complete() {
							// setTimeout(() => {
							// 	//数据
							// 	uni.navigateBack()
							// }, 1000)
						}
					});
				}
			}
		}

	}
</script>

<style>
	@import url("@/components/uParse/src/wxParse.css");

	.uni-padding-wrap {
		display: flex;
		justify-content: center;
		padding: 10px;
	}

	.uni-title {
		height: 480upx;
	}

	.headpic {
		width: 100%;
		flex: 1;
		height: 100%;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.picsrc {
		width: 130upx;
		height: 130upx;
	}
	.flex-item{
		width:100%;
	}
	.uni-common-mt {
		margin-top:0rpx;
		background: #fff;

	}

</style>
